package models;

import java.io.Serializable;
import javax.persistence.*;

import repository.AbstractRepository;

import models.CartItem.Status;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * The persistent class for the cart database table.
 * 
 */
@Entity
@Table(name = "CART")
public class Cart implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @TableGenerator(
            name = "CART_SEQ",
            table = "SEQUENCE",
            pkColumnName = "NAME",
            pkColumnValue = "CART_SEQ",
            valueColumnName = "CNT",
            initialValue = 10000,
            allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "CART_SEQ")
    @Column(unique = true, nullable = false)
    private Integer id;

    private Integer aid;

    private double amount;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_D")
    private Date createD;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "EXPIRE_D")
    private Date expireD;

    @Column(name = "ORDER_ID")
    private Integer orderId;

    private int status;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATE_D")
    private Date updateD;

    @Transient
    private Map<Integer, CartItem> items = null;

    public Cart() {
    }

    public Cart(Integer aid, Double sumAmount) {
        this.aid = aid;
        this.amount = sumAmount;
        this.status = Status.PENDING.ordinal();
    }

    public Map<Integer, CartItem> getMapItems()  {
        if (items == null) {
            List<CartItem> items = getListItems();
            Map<Integer, CartItem> mapItems = new HashMap<Integer, CartItem>();
            for (CartItem item : items) {
                mapItems.put(item.getLoanId(), item);
            }
            this.items = mapItems;
        }
        return items;
    }

    public List<CartItem> getListItems()  {

        List<CartItem> items = AbstractRepository.getListByProperty(CartItem.class, "cartId", this.id);
        return items;
    }

    public void setItems(Map<Integer, CartItem> items) {
        this.items = items;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAid() {
        return this.aid;
    }

    public void setAid(Integer aid) {
        this.aid = aid;
    }

    public double getAmount() {
        return this.amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public Date getCreateD() {
        return this.createD;
    }

    public void setCreateD(Date createD) {
        this.createD = createD;
    }

    public Date getExpireD() {
        return this.expireD;
    }

    public void setExpireD(Date expireD) {
        this.expireD = expireD;
    }

    public Integer getOrderId() {
        return this.orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public Status getStatus() {
        Status[] vals = Status.values();
        if (this.status > vals.length - 1 || this.status < 0)
            return null;
        return Status.values()[this.status];
    }

    public void setStatus(Status status) {
        this.status = status.ordinal();
    }

    public Date getUpdateD() {
        return this.updateD;
    }

    public void setUpdateD(Date updateD) {
        this.updateD = updateD;
    }

    public enum Status {
        PENDING, // 0
        CHECK_OUT, // 1
        EXPIRED// 2
    }

}